package com.elysia.common.validator.ConstraintValidator;

import com.elysia.common.validator.valid.SpecifiedInteger;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

public class SpecifiedIntegerConstraintValidator implements ConstraintValidator<SpecifiedInteger,Integer>{
    private Set<Integer> set=new HashSet<>();
    @Override
    public void initialize(SpecifiedInteger constraintAnnotation) {
        int[] values=constraintAnnotation.vals();
        for (int value:values) {
            set.add(value);
        }
    }

    /**
     *
     * @param integer 想要校验的值
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
